<?php
namespace App\Services;
use App\Models\BreedUserCoinQuota;
use App\Models\BreedCoinQuotaFlow;
use App\Models\HhmCoinSet;
use App\Models\LoveMember as MemberLove;
use App\Models\HhmCoinCorpQuota;
use App\Models\Store;
use Illuminate\Support\Facades\DB;
use App\Models\LoveRecords;
use App\Models\BreedUserCoin;
use App\Models\BreedCoinFlow;
use App\Models\HhmCoinCorpQuotaFlows;
class CropQuotaService
{
    public static function coinReturn($user_id,$outType){
        $store = Store::where('uid',$user_id)->first();
        if(!$store){
            return ;
        }
        $CorpQuota = HhmCoinCorpQuota::where('store_id',$store->id)
        ->where('status',2)
        ->first();
        if(!$CorpQuota){
            return ;
        }
        $retuen_num = $CorpQuota->num - $CorpQuota->stock_num;
        if(!$retuen_num){
            return;
        }
        if($outType ==133){  //茶包还款
            $stock_num = $CorpQuota->stock_num;
            $stock_num_new = 0;
            $stock_num_old = $stock_num;
            $memberLove = MemberLove::where('member_id', $user_id)->first();
            if($memberLove){
                $usable = $memberLove->usable;
                if($usable>0){
                    if($usable > $retuen_num){
                    $old_value = $usable;
                    $num = $retuen_num;
                    $new_value = $old_value-$num;
                    MemberLove::where('member_id', $user_id)
                    ->update(['usable'=>$new_value]);
                    $stock_num_new = $stock_num_old+$retuen_num;
                    $retuen_num = 0;
                    }
                    else{
                        $old_value = $usable;
                        $num = $old_value;
                        $new_value = 0;
                        MemberLove::where('member_id', $user_id)
                        ->update(['usable'=>$new_value]);
                        $retuen_num = $retuen_num - $old_value;
                        $stock_num_new = $stock_num_old+$old_value;
                    }
                    // 创建流水记录
                LoveRecords::create([
                    'uniacid' => 1,
                    'member_id' => $user_id,
                    'old_value' => $old_value,
                    'change_value' => '-'.$num,
                    'new_value' => $new_value,
                    'value_type' => 1,
                    'type' => 2,
                    'source' => 18,
                    'relation' => '',
                    'operator' => 2,
                    'operator_id' => 0,
                    'remark' => '额度归还',
                ]);
                //记录云仓流水
                HhmCoinCorpQuotaFlows::create([
                    "uniacid"=>1, 
                    'send_user' => 0,
                    'rec_user'=>$user_id,
                    'num'=>$num,
                    'type'=>6,
                    'old_coin'=>$stock_num_old,
                    'new_coin'=>$stock_num_new,
                    'remark'=>'茶包归还云仓',
                    'created_at'=>time(),
                    'updated_at'=>time(),
                ]);
                $CorpQuota->stock_num = $stock_num_new;
                if($retuen_num == 0 && $CorpQuota->stock_status==1){ //
                    $CorpQuota->stock_num = 0;
                    $CorpQuota->status = 0;
                    $CorpQuota->stock_status = 0;
                }
                $CorpQuota->save();
                }
            }
        }
        if($outType ==137){ //沐浴露还款
            $value = $retuen_num;
            $stock_num = $CorpQuota->stock_num;
            $stock_num_new = 0;
            $stock_num_old = $stock_num;
            $CoinSet = HhmCoinSet::where('id',1)->first();
            $corp_myl_return = $CoinSet->corp_myl_return;
            $records = json_decode($corp_myl_return,true);
            usort($records, function($a, $b) {
                return ($a["sort"] > $b["sort"]) ? 1 : -1;
            });
            if(count($records)!=0){
                foreach($records as $item){
                    $consume_type = $item['id'];
                    $record = BreedUserCoin::where(['user_id'=>$user_id,'consume_type'=>$consume_type])->first();
                    $num = $record->num;
                    if($record && $retuen_num!=0 && $num!=0){
                        if($num>$retuen_num ){
                            $old_coin = $num;
                            $record->num = $num - $retuen_num;
                            $new_coin = $num - $retuen_num;
                            $record->save();
                            BreedCoinFlow::create(
                                ["uniacid"=>1, 
                                'send_user' => 0,
                                'rec_user'=>$user_id,
                                'num'=>$retuen_num,
                                'type'=>21,
                                'consume_type'=>$consume_type,
                                'old_coin'=>$old_coin,
                                'new_coin'=>$new_coin,
                                'remark'=>'额度归还',
                                'created_at' => time(),
                                'updated_at' => time(),
                                ]
                            );
                            $retuen_num = 0;
                            break;
                        }else{
                            $retuen_num = $retuen_num - $num;
                            $old_coin = $record->num;
                            $record->num = 0;
                            $record->save();
                            BreedCoinFlow::create(
                                ["uniacid"=>1, 
                                'send_user' => 0,
                                'rec_user'=>$user_id,
                                'num'=>$old_coin,
                                'type'=>21,
                                'consume_type'=>$consume_type,
                                'old_coin'=>$old_coin,
                                'new_coin'=>0,
                                'remark'=>'额度归还',
                                'created_at' => time(),
                                'updated_at' => time(),
                                ]
                            );
    
                        }
                    }
                }
                $value = $value - $retuen_num;
                //记录云仓流水
                HhmCoinCorpQuotaFlows::create([
                    "uniacid"=>1, 
                    'send_user' => 0,
                    'rec_user'=>$user_id,
                    'num'=>$value,
                    'type'=>6,
                    'old_coin'=>$stock_num_old,
                    'new_coin'=>$stock_num_old + $value,
                    'remark'=>'沐浴露归还云仓',
                    'created_at'=>time(),
                    'updated_at'=>time(),
                ]);
                $CorpQuota->stock_num = $CorpQuota->num - $retuen_num;
                if($retuen_num ==0 && $CorpQuota->stock_status==1){
                    $CorpQuota->stock_num = 0;
                    $CorpQuota->status = 0;
                    $CorpQuota->stock_status = 0;
                }
                $CorpQuota->save();
            }
        }
        if($outType ==138){ //额度还款
            $stock_num = $CorpQuota->stock_num;
            $stock_num_old = $stock_num;
            $stock_num_new = 0;
            //第一步，扣除额度
            $CoinQuota = BreedUserCoinQuota::where('user_id',$user_id)->first();
            if($CoinQuota){
                $CoinQuota_num = $CoinQuota->num;
                if($CoinQuota_num>0){
                    if($CoinQuota_num > $retuen_num){
                    $old_value = $CoinQuota_num;
                    $num = $retuen_num;
                    $new_value = $CoinQuota_num-$num;
                    $stock_num_new = $stock_num+$retuen_num;
                    $retuen_num = 0;
                    }else{
                    $old_value = $CoinQuota_num;
                    $num = $old_value;
                    $new_value = 0;
                    $stock_num_new = $stock_num+$old_value;
                    $retuen_num = $retuen_num - $old_value;
                    }

                    // 更新额度余额
                    BreedUserCoinQuota::where('user_id', $user_id)->update(['num' => $new_value]);
                    // 创建流水记录
                    BreedCoinQuotaFlow::create([
                        "uniacid"=>1, 
                        'send_user' => 0,
                        'rec_user'=>$user_id,
                        'num'=>$old_value,
                        'type'=>6,
                        'old_coin'=>$old_value,
                        'new_coin'=>$new_value,
                        'remark'=>'云仓库存额度归还',
                        'created_at'=>time(),
                        'updated_at'=>time(),
                    ]);
                    //记录云仓流水
                    HhmCoinCorpQuotaFlows::create([
                        "uniacid"=>1, 
                        'send_user' => 0,
                        'rec_user'=>$user_id,
                        'num'=>$num,
                        'type'=>6,
                        'old_coin'=>$stock_num_old,
                        'new_coin'=>$stock_num_new,
                        'remark'=>'额度归还云仓',
                        'created_at'=>time(),
                        'updated_at'=>time(),
                    ]);
                    $CorpQuota->stock_num = $stock_num_new;
                    if($retuen_num == 0 && $CorpQuota->stock_status==1){
                        $CorpQuota->stock_num = 0;
                        $CorpQuota->status = 0;
                        $CorpQuota->stock_status = 0;
                    }
                    $CorpQuota->save();
                }
            }
        }
    }
    public static function coinReturnAuto($user_id)
    {   
        $store = Store::where('uid',$user_id)->first();
        $CorpQuota = HhmCoinCorpQuota::where('store_id',$store->id)
        ->where('status',2)
        ->first();
        
        if(!$CorpQuota){
            return;
        }
        $retuen_num = $CorpQuota->num - $CorpQuota->stock_num;
        if(!$retuen_num){
            return;
        }
        $stock_num = $CorpQuota->stock_num;
        $stock_num_old = 0;
        $stock_num_new = 0;
        //系统自动依次扣除店长的额度，茶包，沐浴露，用来还企业额度
        //第一步，扣除额度
        $CoinQuota = BreedUserCoinQuota::where('user_id',$user_id)->first();
        if($CoinQuota){
            $CoinQuota_num = $CoinQuota->num;
            if($CoinQuota_num>0){
                if($CoinQuota_num > $retuen_num){
                $old_value = $CoinQuota->num;
                $num = $retuen_num;
                $new_value = $old_value-$num;
                $stock_num_old = $stock_num;
                $stock_num_new = $stock_num+$retuen_num;
                $retuen_num = 0;
                }else{
                $old_value = $CoinQuota->num;
                $num = $old_value;
                $new_value = 0;
                $stock_num_old = $stock_num;
                $stock_num_new = $stock_num+$old_value;
                $retuen_num = $retuen_num - $old_value;
                }
                // 更新额度余额
                BreedUserCoinQuota::where('user_id', $user_id)->update(['num' => $new_value]);
                // 创建流水记录
                BreedCoinQuotaFlow::create([
                    "uniacid"=>1, 
                    'send_user' => 0,
                    'rec_user'=>$user_id,
                    'num'=>$old_value,
                    'type'=>6,
                    'old_coin'=>$old_value,
                    'new_coin'=>$new_value,
                    'remark'=>'云仓库存额度归还',
                    'created_at'=>time(),
                    'updated_at'=>time(),
                ]);
                //记录云仓流水
                HhmCoinCorpQuotaFlows::create([
                    "uniacid"=>1, 
                    'send_user' => 0,
                    'rec_user'=>$user_id,
                    'num'=>$num,
                    'type'=>6,
                    'old_coin'=>$stock_num_old,
                    'new_coin'=>$stock_num_new,
                    'remark'=>'额度归还云仓',
                    'created_at'=>time(),
                    'updated_at'=>time(),
                ]);
            }
        }
        if($retuen_num ==0){
            $CorpQuota->stock_num = 0;
            $CorpQuota->status = 0;
            $CorpQuota->stock_status = 0;
            $CorpQuota->save();
            return;
        }
        //第二步，扣除茶包
        $stock_num_old = $stock_num_new;
        $memberLove = MemberLove::where('member_id', $user_id)->first();
        if($memberLove){
            $usable = $memberLove->usable;
            if($usable>0){
                if($usable > $retuen_num){
                $old_value = $usable;
                $num = $retuen_num;
                $new_value = $old_value-$num;
                MemberLove::where('member_id', $user_id)
                ->update(['usable'=>$new_value]);
                $stock_num_new = $stock_num_old+$retuen_num;
                $retuen_num = 0;
                }
                else{
                    $old_value = $usable;
                    $num = $old_value;
                    $new_value = 0;
                    MemberLove::where('member_id', $user_id)
                    ->update(['usable'=>$new_value]);
                    $retuen_num = $retuen_num - $old_value;
                    $stock_num_new = $stock_num_old+$old_value;
                }
                // 创建流水记录
            LoveRecords::create([
                'uniacid' => 1,
                'member_id' => $user_id,
                'old_value' => $old_value,
                'change_value' => '-'.$num,
                'new_value' => $new_value,
                'value_type' => 1,
                'type' => 2,
                'source' => 18,
                'relation' => '',
                'operator' => 2,
                'operator_id' => 0,
                'remark' => '额度归还',
            ]);
            //记录云仓流水
            HhmCoinCorpQuotaFlows::create([
                "uniacid"=>1, 
                'send_user' => 0,
                'rec_user'=>$user_id,
                'num'=>$num,
                'type'=>6,
                'old_coin'=>$stock_num_old,
                'new_coin'=>$stock_num_new,
                'remark'=>'茶包归还云仓',
                'created_at'=>time(),
                'updated_at'=>time(),
            ]);
            }
        }
            if($retuen_num ==0){
                $CorpQuota->stock_num = 0;
                $CorpQuota->status = 0;
                $CorpQuota->stock_status = 0;
                $CorpQuota->save();
                return;
            }
            //第三步，扣除沐浴露
            $value = $retuen_num;
            $stock_num_old = $stock_num_new;
            $CoinSet = HhmCoinSet::where('id',1)->first();
            $corp_myl_return = $CoinSet->corp_myl_return;
            $records = json_decode($corp_myl_return,true);
            usort($records, function($a, $b) {
                return ($a["sort"] > $b["sort"]) ? 1 : -1;
            });
            if(count($records)!=0){
                foreach($records as $item){
                    $consume_type = $item['id'];
                    $record = BreedUserCoin::where(['user_id'=>$user_id,'consume_type'=>$consume_type])->first();
                    $num = $record->num;
                    if($record && $retuen_num!=0 && $num!=0){
                        if($num>$retuen_num ){
                            $record->num = $num - $retuen_num;
                            $old_coin = $num;
                            $new_coin = $num - $retuen_num;
                            $record->save();
                            BreedCoinFlow::create(
                                ["uniacid"=>1, 
                                'send_user' => 0,
                                'rec_user'=>$user_id,
                                'num'=>$retuen_num,
                                'type'=>21,
                                'consume_type'=>$consume_type,
                                'old_coin'=>$old_coin,
                                'new_coin'=>$new_coin,
                                'remark'=>'额度归还',
                                'created_at' => time(),
                                'updated_at' => time(),
                                ]
                            );
                            $retuen_num = 0;
                            break;
                        }else{
                            $retuen_num = $retuen_num - $num;
                            $old_coin = $record->num;
                            $record->num = 0;
                            $record->save();
                            BreedCoinFlow::create(
                                ["uniacid"=>1, 
                                'send_user' => 0,
                                'rec_user'=>$user_id,
                                'num'=>$old_coin,
                                'type'=>21,
                                'consume_type'=>$consume_type,
                                'old_coin'=>$old_coin,
                                'new_coin'=>0,
                                'remark'=>'额度归还',
                                'created_at' => time(),
                                'updated_at' => time(),
                                ]
                            );
    
                        }
                    }
                }
                $value = $value - $retuen_num;
                //记录云仓流水
                HhmCoinCorpQuotaFlows::create([
                    "uniacid"=>1, 
                    'send_user' => 0,
                    'rec_user'=>$user_id,
                    'num'=>$value,
                    'type'=>6,
                    'old_coin'=>$stock_num_old,
                    'new_coin'=>$stock_num_old + $value,
                    'remark'=>'沐浴露归还云仓',
                    'created_at'=>time(),
                    'updated_at'=>time(),
                ]);
            }
                $CorpQuota->stock_num = $CorpQuota->num - $retuen_num;
                if($retuen_num ==0){
                    $CorpQuota->stock_num = 0;
                    $CorpQuota->status = 0;
                    $CorpQuota->stock_status = 0;
                }
                $CorpQuota->save();
    }
    
    public static function coinReturnSchedule()
    {   

        DB::transaction(function ()  {
            $CoinSet = HhmCoinSet::where('id',1)->first();
            $corp_quota_active_time = $CoinSet->corp_quota_active_time;
            $now = time();
            $CorpQuotas = HhmCoinCorpQuota::whereNotNull('audit_passed_at')
            ->where('audit_passed_at','<=',$now - (3600*24*$corp_quota_active_time))
            ->where('stock_status',0)
            ->where('status',2)
            ->get();
            \Log::info($CorpQuotas);
            foreach($CorpQuotas as $item){
                if($item->stock_num == $item->num){
                    HhmCoinCorpQuota::where('id',$item->id)->update(['stock_status'=>0,'status'=>0,'stock_num'=>0]);
                }else{
                    HhmCoinCorpQuota::where('id',$item->id)->update(['stock_status'=>1]);
                    $store = Store::where('id',$item->store_id)->first();
                    $user_id = $store->uid;
                    self::coinReturnAuto($user_id);
                }
               
            }
        });
        
    }


}